<?php

namespace app\model\v1\repair;


use app\model\user\User;
use app\model\v1\recovery\ConfigAbout;
use app\model\v1\recovery\ConfigBanner;
use constants\ProductConstants;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use think\Model;

class RepairConsult extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_repair_consult';

    const VERSION_TYPE = 1;
    const MSG_TEXT_TYPE = 2;
    const AGREMENT_TYPE = 3;
    const PRIVACY_TYPE = 4;





    /**
     * 咨询管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getRepairConsultList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
                when status=0 then '禁用' 
                end as status_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),  strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("title",'like', '%'.$param["word"].'%');
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),  strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("title",'like', '%'.$param["word"].'%');
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->count();

        return $data;
    }



    /**
     * 咨询管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateRepairConsult($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["cate_id"]))       $update["cate_id"] = $param["cate_id"];
        if(!empty($param["content"]))       $update["content"] = $param["content"];
        if(!empty($param["image"]))         $update["image"] = $param["image"];
        if(!empty($param["title"]))         $update["title"] = $param["title"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 咨询管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addRepairConsult($param = [])
    {
        $add = [
            "cate_id"   => $param["cate_id"] ?? '',
            "image"   => $param["image"] ?? '',
            "title"      => $param["title"] ?? '',
            "create_time"  => time(),
        ];
        return self::insert($add);
    }



    /**
     * 咨询管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableRepairConsult($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }

}